package com.qingxin.wuye.repertory.service;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.repertory.entity.ChukuDetail;
import com.qingxin.wuye.repertory.entity.Gi;

@Service
public class ChukuDetailService extends BaseService<ChukuDetail, String>{
	@Autowired
	private GiService giService;
	
	/**
	 * 列表页
	 * @param searchname
	 * @param docketid
	 * @param repertoryid
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String searchname,  String repertoryid ,String docketid ,PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.repertory.entity.ChukuDetail(" +
				"r.detailid,r.docketid,r.repertoryid,r.resid, r.resCode, r.resName, r.num, r.price, r.amount, r.remark)" +
				" from ChukuDetail r" +
				" where 1=1");
		
		if (StringUtils.isNotEmpty(docketid)) {
			querier.append(" and r.docketid in (select t.docketid from ChukuDocket t where t.docketid =:docketid) ");
			querier.setParam("docketid",docketid);
		}
			
		if (StringUtils.isNotEmpty(repertoryid)) {
			querier.append(" and r.repertoryid=:repertoryid");
			querier.setParam("repertoryid",repertoryid);
		}

		if(StringUtils.isNotEmpty(searchname)){
			searchname = searchname.replace("'", "");
			querier.append(" and r,resName like :resName ");
			querier.setParam("resName", "%"+searchname+"%");
		}
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 添加
	 * @param rukuDetail
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(ChukuDetail chukuDetail, LoginUser loginUser, HttpServletRequest request){
		Object obj = getByHql("select detailid from ChukuDetail where resid=? and docketid=?", chukuDetail.getResid(),chukuDetail.getDocketid());
		if (obj != null) {
			return new SysError("该商品已存在明细");
		}
		
		
		//减少库存
		Gi gi = (Gi) giService.getByHql("from Gi where resid = ? and repertoryid = ?", chukuDetail.getResid(),chukuDetail.getRepertoryid());
		if (gi != null) {
			if (chukuDetail.getNum() > gi.getGinum()) {
				return new SysError("出库数量不能大于库存剩余量");
			}
			
			if (gi.getOutNum() != null) {
				gi.setOutNum(chukuDetail.getNum()+gi.getOutNum());
			}else {
				gi.setOutNum(chukuDetail.getNum());
			}
			
			gi.setGinum(gi.getGinum() - chukuDetail.getNum());
			
			
			giService.update(gi, loginUser, request);
		}
		
		chukuDetail.setCreateTime(new Date());
		chukuDetail.setCreateBy(loginUser.getLogin_name());
		this.save(chukuDetail);
		return null;
	}
	
	/**
	 * 更新
	 * @param rukuDetail
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(ChukuDetail chukuDetail, LoginUser loginUser, HttpServletRequest request){
		Object obj = getByHql("select detailid from ChukuDetail where detailid<>? and  resid=? and docketid=?",chukuDetail.getDetailid(), chukuDetail.getResid(),chukuDetail.getDocketid());
		if (obj != null) {
			return new SysError("该商品已存在明细");
		}else {
			ChukuDetail old = getSafeEntity(chukuDetail.getDetailid());
			
			//如果更新的时候更新了商品
			if (!chukuDetail.getResid().equals(old.getResid())) {
				
				//修改后的商品的库存处理
				Gi gi = (Gi) giService.getByHql("from Gi where resid = ? and repertoryid = ?", chukuDetail.getResid(),chukuDetail.getRepertoryid());
				ChukuDetail oldNew = (ChukuDetail) getByHql("from ChukuDetail where resid = ? and repertoryid = ? and docketid = ?", chukuDetail.getResid(),chukuDetail.getRepertoryid(),chukuDetail.getDocketid());
				if (oldNew != null) {//修改后的商品有出库明细
					return new SysError("该物品存在明细");
				}
				if (chukuDetail.getNum() > gi.getGinum()) {
					return new SysError("出库数量不能大于库存剩余量");
				}

				if (chukuDetail.getNum() < gi.getGinum()) {
					gi.setOutNum(chukuDetail.getNum());
					gi.setGinum(gi.getGinum() - chukuDetail.getNum());
				}
				giService.update(gi, loginUser, request);
				//修改前的商品的库存
				Gi giold = (Gi) giService.getByHql("from Gi where resid = ? and repertoryid = ?", old.getResid(),old.getRepertoryid());
				/*if (old.getNum() > giold.getGinum()) {
					return new SysError("出库数量不能大于库存剩余量");
		 		}*/
				giold.setOutNum(giold.getOutNum() - old.getNum());
				giold.setGinum(giold.getGinum() + old.getNum());
				giService.update(giold, loginUser, request);
				String fields = "resid,resCode,resName,num,price,amount,remark";
				BeanUtil.setTargetFields(old, chukuDetail, fields, true);
				Updater<ChukuDetail> updater = new Updater<>(old, fields);
				this.update(updater);
			}else {
				//更新库存
				
				Gi gi = (Gi) giService.getByHql("from Gi where resid = ? and repertoryid = ?", old.getResid(),old.getRepertoryid());
				if (chukuDetail.getNum() > (gi.getGinum()+old.getNum())) {
					return new SysError("出库数量不能大于库存剩余量");
				}
			
				Integer editNum2 = 0;
				if (chukuDetail.getNum() > old.getNum()) {
					editNum2 = chukuDetail.getNum() - old.getNum();
					gi.setOutNum(gi.getOutNum() + editNum2);
					gi.setGinum(gi.getGinum() - editNum2);
					
				}
				if (chukuDetail.getNum() < old.getNum()) {
					editNum2 = old.getNum() - chukuDetail.getNum();
					gi.setOutNum(gi.getOutNum() - editNum2);
					gi.setGinum(gi.getGinum() + editNum2);
				}
				giService.update(gi, loginUser, request);
				String fields = "resid,resCode,resName,num,price,amount,remark";
				BeanUtil.setTargetFields(old, chukuDetail, fields, true);
				Updater<ChukuDetail> updater = new Updater<>(old, fields);
				this.update(updater);
			}
		}
		return null;
	}
	

	
	
	public Object delete(String[] detailid, LoginUser loginUser, HttpServletRequest request){
		for(String id : detailid){
			ChukuDetail chukuDetail = get(id);
			if(chukuDetail==null){
				continue;
			}
			
			Gi gi = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", chukuDetail.getRepertoryid(),chukuDetail.getResid());
			if (gi != null) {
				gi.setGinum(gi.getGinum() + chukuDetail.getNum());
				gi.setOutNum(gi.getOutNum() - chukuDetail.getNum());
				giService.update(gi, loginUser, request);
				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>删除出库明细");
			}
			
			this.delete(chukuDetail);
		}
		return null;
	}
	
}
